package top.kaoshanji.p1ch09d;

/**
 * 代码清单-p1ch09d-215页a：协变返回类型
 * author kaoshanji
 * ^_^
 * create time 2025/3/9 19:08
 */
public class CovariantReturn {

    public static void main(String[] args) {
        Mill m = new Mill();
        Grain g = m.process();

        System.out.println(g);

        m = new WheatMill();
        g = m.process();

        System.out.println(g);
    }

}

class Grain {

    @Override
    public String toString() {
        return "Grain";
    }
}

class Wheat extends Grain {

    @Override
    public String toString() {
        return "Wheat";
    }
}

class Mill {

    Grain process() {
        return new Grain();
    }
}

class WheatMill extends Mill {

    // 重写基类，但是返回类型是子类型
    @Override
    Wheat process() {
        return new Wheat();
    }
}